<?php

use yii\db\Migration;

/**
 * Class m190105_064721_create_user_card_recharge
 */
class m190105_064721_create_user_card_recharge extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('{{%user_card_recharge}}', [
            'id' => $this->primaryKey(),
            'uid' => $this->integer()->comment('用户编号'),
            'agent_id' => $this->integer()->comment('代理商编号'),
            'card_id' => $this->integer()->notNull()->comment('卡编号'),
            'package_id' => $this->integer()->notNull()->comment('套餐编号'),
            'term_length' => $this->integer()->notNull()->defaultValue(0)->comment('套餐有效期'),
            'term_length_unit' => $this->integer()->notNull()->comment('套餐有效期单位'),
            'price' => $this->decimal(20, 4)->notNull()->defaultValue(0)->comment('单价'),
            'money' => $this->decimal(20, 4)->notNull()->defaultValue(0)->comment('金额'),
            'trade_type' => $this->integer()->notNull()->comment('支付方式'),
            'trade_no' => $this->string(128)->notNull()->comment('平台交易流水号'),
            'trans_id' => $this->string(128)->comment('第三方交易支付流水号'),
            'status' => $this->integer()->notNull()->comment('状态'),
            'create_time' => $this->integer()->notNull()->comment('创建时间'),
            'pay_time' => $this->integer()->comment('支付时间'),
            'update_time' => $this->integer()->comment('卡更新时间'),
            'input' => $this->text()->comment('第三方支付输入'),
            'output' => $this->text()->comment('第三方支付输出'),
            'notify' => $this->text()->comment('第三方支付回调'),
            'remark' => $this->text()->comment('备注'),
        ]);
        $this->createIndex('fk_user_card_recharge_user1_idx', '{{%user_card_recharge}}', ['uid']);
        $this->addForeignKey('fk_user_card_recharge_user1', '{{%user_card_recharge}}', ['uid'], '{{%user}}', ['id']);
        $this->createIndex('fk_user_card_recharge_agent1_idx', '{{%user_card_recharge}}', ['agent_id']);
        $this->addForeignKey('fk_user_card_recharge_agent1', '{{%user_card_recharge}}', ['agent_id'], '{{%agent}}', ['id']);
        $this->createIndex('fk_user_card_recharge_card1_idx', '{{%user_card_recharge}}', ['card_id']);
        $this->addForeignKey('fk_user_card_recharge_card1', '{{%user_card_recharge}}', ['card_id'], '{{%card}}', ['id']);
        $this->createIndex('fk_user_card_recharge_package1_idx', '{{%user_card_recharge}}', ['package_id']);
        $this->addForeignKey('fk_user_card_recharge_package1', '{{%user_card_recharge}}', ['package_id'], '{{%package}}', ['id']);
        $this->createIndex('user_card_recharge1_idx', '{{%user_card_recharge}}', ['trade_no'], true);
        $this->batchInsert('{{%key_map}}', ['t', 'k', 'v'], [
            ['user_card_recharge_trade_type', 1, '微信JSAPI'],
            ['user_card_recharge_status', 1, '待支付'],
            ['user_card_recharge_status', 2, '已支付待充值'],
            ['user_card_recharge_status', 3, '已充值'],
            ['user_card_recharge_status', 0, '已删除'],
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->delete('{{%key_map}}', ['t' => 'user_card_recharge_status']);
        $this->delete('{{%key_map}}', ['t' => 'user_card_recharge_trade_type']);
        $this->dropTable('{{%user_card_recharge}}');
    }
}
